package com.zhixi.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhixi.common.R;
import com.zhixi.dto.SetmealDto;
import com.zhixi.pojo.Category;
import com.zhixi.pojo.Setmeal;
import com.zhixi.pojo.SetmealDish;
import com.zhixi.service.CategoryService;
import com.zhixi.service.SetmealDishService;
import com.zhixi.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName SetmealController
 * @Author zhangzhixi
 * @Description 菜品套餐控制器
 * @Date 2022-7-13 14:23
 * @Version 1.0
 */
@Slf4j
@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private SetmealDishService setmealDishService;

    @Autowired
    private CategoryService categoryService;

    /**
     * 新增套餐
     *
     * @param setmealDto 套餐数据传输对象
     * @return R结果对象
     */
    @PostMapping
    @CacheEvict(cacheNames = "setmealCache", allEntries = true)
    public R<String> saveSeteal(@RequestBody SetmealDto setmealDto, HttpServletRequest request) {
        log.info("新增套餐：{}", setmealDto);
        setmealService.saveSetmeal(setmealDto, request);
        return R.success("套餐保存成功");
    }


    /**
     * 查询套餐列表-使用DTO返回结果
     * @param page 分页对象
     * @param pageSize 分页大小
     * @param name 套餐名称
     * @return R结果对象
     *//*
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, @RequestParam(required = false) String name) {
        log.info("page = {}, pageSize = {}, name = {}",page,pageSize,name);
        // 1、构造分页对象
        Page<Setmeal> setmealPage = new Page<>(page, pageSize);
        Page<SetmealDto> setmealDtoPage = new Page<>(page, pageSize);

        // 2、构造查询对象
        LambdaQueryWrapper<Setmeal> setmealQueryWrapper = new LambdaQueryWrapper<>();
        setmealQueryWrapper.like(name != null, Setmeal::getName, name)
                .orderByDesc(Setmeal::getUpdateTime);
        setmealPage = setmealService.page(setmealPage, setmealQueryWrapper);

        // 3、拷贝分页数据，不包含等下要处理的数据
        BeanUtils.copyProperties(setmealPage, setmealDtoPage, "records");

        // 4、处理数据
        List<SetmealDto> setmealList = setmealPage.getRecords().stream().map((item) -> {
            // 最后要进行返回的数据对象
            SetmealDto setmealDto = new SetmealDto();

            // 给DTO对象的属性赋值
            BeanUtils.copyProperties(item,setmealDto);
            Category category = categoryService.getById(item.getCategoryId());
            if (category != null) {
                setmealDto.setCategoryName(category.getName());
            }
            return setmealDto;
        }).collect(Collectors.toList());


        // 5、将数据重新设置到page对象中
        setmealDtoPage.setRecords(setmealList);

        return R.success(setmealDtoPage);
    }*/

    /**
     * 查询套餐列表，使用在实体类中添加新属性的方式，进行数据的封装
     *
     * @param page     分页对象
     * @param pageSize 分页大小
     * @param name     套餐名称
     * @return R结果对象
     */
    @GetMapping("/page")
    public R<Page<Setmeal>> page(int page, int pageSize, @RequestParam(required = false) String name) {
        log.info("page = {}, pageSize = {}, name = {}", page, pageSize, name);
        Page<Setmeal> setmealPage = setmealService.pageWithSetmealDto(page, pageSize, name);
        return R.success(setmealPage);
    }

    /**
     * 获取套餐
     *
     * @param id 套餐id
     * @return R结果对象
     */
    @GetMapping("/{id}")
    public R<Setmeal> getSetmeal(@PathVariable("id") Long id) {
        Setmeal setmeal = setmealService.getSetmeal(id);
        return R.success(setmeal);
    }


    /**
     * 删除套餐
     *
     * @param ids 套餐ID集合
     * @return R结果对象
     */
    @DeleteMapping
    @CacheEvict(cacheNames = "setmealCache", allEntries = true)
    public R<String> deleteSetmeal(Long[] ids) {
        return setmealService.delWithSetmealDish(ids);
    }

    /**
     * 更新套餐
     *
     * @param status  状态
     * @param id      套餐ID
     * @param request 请求对象
     * @return R结果对象
     */
    @CachePut(cacheNames = "setmealCache", key = "#id")
    @PostMapping("/status/{status}")
    public R<String> updateDishStatus(@PathVariable("status") int status, @RequestParam("ids") Long[] id, HttpServletRequest request) {
        // 修改菜品状态为下架
        setmealService.updateStatus(id, status, request);
        return R.success("菜品状态修改成功");
    }

    /**
     * 更新套餐
     *
     * @param setmeal 套餐对象
     * @return R结果对象
     */
    @PutMapping
    @CachePut(cacheNames = "setmealCache", key = "#id")
    public R<String> updateSetmeal(@RequestBody Setmeal setmeal) {
        return setmealService.updateWithSetmealDish(setmeal);
    }


    /**
     * 移动端-查询套餐列表
     *
     * @param categoryId 分类id
     * @param status     状态，0：未上架，1：已上架
     * @return R结果对象
     */
    @GetMapping("/list")
    @Cacheable(cacheNames = "setmealCache", key = "#categoryId + ':' + #status")
    public R<List<Setmeal>> getSetmealList(Long categoryId, Long status) {
        List<Setmeal> getSetmealList = setmealService.getSetmealList(categoryId, status);
        return R.success(getSetmealList);

    }

    /**
     * 移动端-查询套餐中有哪些菜品
     *
     * @param setMealId 套餐id
     * @return R结果对象
     */
    @GetMapping("/dish/{id}")
    @Cacheable(cacheNames = "setmealCache", key = "#root.methodName+'_'+#setMealId")
    public R<List<SetmealDish>> getSetmealDishList(@PathVariable("id") Long setMealId) {
        List<SetmealDish> setmealDishList = setmealDishService.getSetmealDishList(setMealId);
        return R.success(setmealDishList);
    }
}

























